import conf from "~/conf"
export  const useAjax: typeof useFetch = (request, opts?) => {
  return useFetch(request, {baseURL: conf.baseURL, ...opts})
}
export  const useServerFetch = async <T>(request: string, opts?: any) => {
  let accessToken = '';
  if (typeof window !== 'undefined' && window.localStorage) {
    const userData = window.localStorage.getItem('userData');
    if (userData) {
      const parsedUserData = JSON.parse(userData);
      accessToken = parsedUserData.accessToken || '';
    }
  }
  const data: { data: T, msg:string, code:number } =  await $fetch(request, {baseURL: conf.baseURL,headers:{
    Authorization:'Bearer ' + accessToken
  }, ...opts});
  if (typeof window !== 'undefined' && window.localStorage) {
    if(data?.code == 401){
      window.localStorage.removeItem('userData');
    }
  }
  return data;
}
